package top.zhangjianyong.tools.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class DateTimeUtil {

    public final static DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public final static DateTimeFormatter yyyyMMdd2 = DateTimeFormatter.ofPattern("yyyyMMdd");
    public final static DateTimeFormatter yyyyMMddHHmm = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    public final static DateTimeFormatter yyyyMMddHHmmss = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public final static DateTimeFormatter yyyyMM = DateTimeFormatter.ofPattern("yyyy-MM");
    public final static DateTimeFormatter yyyyMM2 = DateTimeFormatter.ofPattern("yyyyMM");
    public final static DateTimeFormatter HHmmss = DateTimeFormatter.ofPattern("HH:mm:ss");

    public static LocalDateTime localDateTime(String dateTime, String format) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(format);
        return LocalDateTime.parse(dateTime, dtf);
    }

    public static LocalTime parseTime(String source) {
        return LocalTime.parse(source, HHmmss);
    }

    public static LocalDate parseDate(String source) {
        return LocalDate.parse(source, yyyyMMdd);
    }

    public static LocalDateTime parseDateTime(String source, DateTimeFormatter dtf) {
        return LocalDateTime.parse(source, dtf);
    }

    public static YearMonth parseMonth(String source) {
        return YearMonth.parse(source, yyyyMM);
    }


    public static String yearMonth() {
        return yyyyMM2.format(YearMonth.now());
    }

    public static void main(String[] args) {
        System.out.println(yearMonth());
    }
}
